package org.edu.modules.kwgl.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.edu.common.system.base.controller.BaseController;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.kwgl.entity.VeJwBkJiankao;
import org.edu.modules.kwgl.entity.VeJwKsJiankao;
import org.edu.modules.kwgl.service.IVeJwKsJiankaoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

 /**
 * @Description: ve_jw_ks_jiankao
 * @Author: jeecg-boot
 * @Date:   2021-05-12
 * @Version: V1.0
 */
@Api(tags="ve_jw_ks_jiankao")
@RestController
@RequestMapping("/kwgl/veJwKsJiankao")
@Slf4j
public class VeJwKsJiankaoController extends BaseController<VeJwKsJiankao, IVeJwKsJiankaoService> {
	@Autowired
	private IVeJwKsJiankaoService veJwKsJiankaoService;
	
	/**
	 * 分页列表查询
	 *
	 * @param veJwKsJiankao
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ve_jw_ks_jiankao-分页列表查询")
	@ApiOperation(value="ve_jw_ks_jiankao-分页列表查询", notes="ve_jw_ks_jiankao-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(VeJwKsJiankao veJwKsJiankao,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<VeJwKsJiankao> queryWrapper = QueryGenerator.initQueryWrapper(veJwKsJiankao, req.getParameterMap());
		Page<VeJwKsJiankao> page = new Page<VeJwKsJiankao>(pageNo, pageSize);
		IPage<VeJwKsJiankao> pageList = veJwKsJiankaoService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param veJwKsJiankao
	 * @return
	 */
	@AutoLog(value = "ve_jw_ks_jiankao-添加")
	@ApiOperation(value="ve_jw_ks_jiankao-添加", notes="ve_jw_ks_jiankao-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody VeJwKsJiankao veJwKsJiankao) {
		veJwKsJiankaoService.save(veJwKsJiankao);
		return Result.ok("添加成功！");
	}



	 /**
	  *  设置监考/巡考老师
	  *
	  * @param veJwKsJiankao
	  * @return
	  */
	 @AutoLog(value = "补考监考-设置监考/巡考老师")
	 @ApiOperation(value="补考监考-设置监考/巡考老师", notes="补考监考-设置监考/巡考老师")
	 @PutMapping(value = "/setTeacher")
	 public Result<?> setTeacher(@RequestBody VeJwKsJiankao veJwKsJiankao) {

		 if(veJwKsJiankao.getTeacherid()==null||veJwKsJiankao.getType()==null||veJwKsJiankao.getKsid()==null){
			 return Result.error("请确保信息完整！");
		 }
		 //获取原信息
		 List<VeJwKsJiankao> tlist = veJwKsJiankaoService.get2teacherByKsid(veJwKsJiankao.getKsid());

		 //监考
		 if(veJwKsJiankao.getType()==1){
			 //如果存在监考信息直接更新
			 if(tlist.get(0)!=null){
				 veJwKsJiankaoService.updateById(tlist.get(0).setTeacherid(veJwKsJiankao.getTeacherid()));
			 }else{
				 veJwKsJiankaoService.save(veJwKsJiankao);
			 }
		 }
		 //巡考
		 if(veJwKsJiankao.getType()==2){
			 //如果存在巡考信息直接更新
			 if(tlist.get(1)!=null){
				 veJwKsJiankaoService.updateById(tlist.get(1).setTeacherid(veJwKsJiankao.getTeacherid()));
			 }else{
				 veJwKsJiankaoService.save(veJwKsJiankao);
			 }
		 }
		 return Result.ok("编辑成功!");
	 }
	
	/**
	 *  编辑
	 *
	 * @param veJwKsJiankao
	 * @return
	 */
	@AutoLog(value = "ve_jw_ks_jiankao-编辑")
	@ApiOperation(value="ve_jw_ks_jiankao-编辑", notes="ve_jw_ks_jiankao-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody VeJwKsJiankao veJwKsJiankao) {
		veJwKsJiankaoService.updateById(veJwKsJiankao);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ve_jw_ks_jiankao-通过id删除")
	@ApiOperation(value="ve_jw_ks_jiankao-通过id删除", notes="ve_jw_ks_jiankao-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		veJwKsJiankaoService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ve_jw_ks_jiankao-批量删除")
	@ApiOperation(value="ve_jw_ks_jiankao-批量删除", notes="ve_jw_ks_jiankao-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.veJwKsJiankaoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ve_jw_ks_jiankao-通过id查询")
	@ApiOperation(value="ve_jw_ks_jiankao-通过id查询", notes="ve_jw_ks_jiankao-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		VeJwKsJiankao veJwKsJiankao = veJwKsJiankaoService.getById(id);
		if(veJwKsJiankao==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(veJwKsJiankao);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param veJwKsJiankao
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VeJwKsJiankao veJwKsJiankao) {
        return super.exportXls(request, veJwKsJiankao, VeJwKsJiankao.class, "ve_jw_ks_jiankao");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, VeJwKsJiankao.class);
    }

}
